﻿Imports System.Data.OleDb
Partial Public Class DiscussionDetails
    Inherits System.Web.UI.Page
    Dim DiscussionID As Integer
    Dim UserID As Int16
    Dim totalComment As Integer
    Dim petID As Integer
    Dim UserName As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        DiscussionID = Request.QueryString("ID")
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()

        Dim oledbcmd As New OleDbCommand("SELECT * From Discussion_tbl where ID = " & DiscussionID & "", con)
        Dim dr As OleDbDataReader = oledbcmd.ExecuteReader()
        dr.Read()
        Me.img_user.ImageUrl = dr("UserImage")
        Me.lbl_UserName.Text = dr("UserName")
        Me.lbl_TOP.Text = dr("Dateofpost")
        Me.lbl_title.Text = dr("Title")
        Me.lbl_Description.Text = dr("Description")
        con.Close()
        dr.Close()
        Me.lbl_totalComments.Text = count_commentonpost()



        LoadPetdetails()



    End Sub
    Private Sub btn_post_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_post.Click
        Insert_post()

    End Sub

    Sub Insert_post()
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()
        Dim oledbcmd As New OleDbCommand("Insert into Discussion_Comments (Discussion_ID_fk,UserName,UserImage,commentofUser,TimeOfComment,DateOfComment) values (@Discussion_ID_fk,UserName,UserImage,commentofUser,TimeOfComment,DateOfComment)", con)

        oledbcmd.Parameters.AddWithValue("Discussion_ID_fk", DiscussionID)
        oledbcmd.Parameters.AddWithValue("UserName", User.Identity.Name)
        oledbcmd.Parameters.AddWithValue("UserImage", findUSerID())
        oledbcmd.Parameters.AddWithValue("commentOfUser", Me.tb_comment.Text)
        oledbcmd.Parameters.AddWithValue("TimeOfComment", Date.Now.ToShortTimeString())
        oledbcmd.Parameters.AddWithValue("DateOfComment", Date.Now.ToShortDateString())

        oledbcmd.ExecuteNonQuery()
        con.Close()


    End Sub

    Function findUSerID()
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()

        Dim oledbcmd As New OleDbCommand("SELECT * From aspnet_Users where UserName = @Username", con)
        oledbcmd.Parameters.AddWithValue("@Username", User.Identity.Name)
        Dim dr As OleDbDataReader = oledbcmd.ExecuteReader()
        dr.Read()
        UserID = dr("UserId")
        Return UserID
    End Function

    Function LoadPetdetails()
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()

        Dim oledbcmd As New OleDbCommand("SELECT * From Discussion_tbl where ID = " & DiscussionID & "", con)
        Dim dr As OleDbDataReader = oledbcmd.ExecuteReader()
        If dr.HasRows Then
            While dr.Read
                petID = dr("petID")
            End While
        End If
        con.Close()
        dr.Close()

        con.Open()
        Dim sqlString As String = "select * from PetDetails where ID=" & petID & ""
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sqlString, con)
        Dim oledbdr As OleDb.OleDbDataReader = cmd.ExecuteReader()
        If oledbdr.HasRows Then
            While oledbdr.Read()
                Me.lbl_petname.Text = oledbdr("PetName")

                Me.img_pet.ImageUrl = oledbdr("photo")
            End While

        End If

        dr.Close()
        con.Close()
        Return 0
    End Function

    Function count_commentonpost()
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()

        Dim oledbcmd As New OleDbCommand("SELECT * From Discussion_Comments where Discussion_ID_fk = " & DiscussionID & "", con)
        Dim dr As OleDbDataReader = oledbcmd.ExecuteReader()
        If dr.HasRows Then
            While dr.Read
                totalComment += 1
            End While

        End If


        Return totalComment

    End Function


  

End Class